package com.budwk.app.waterquality.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.app.waterquality.config.AppException;
import com.budwk.app.waterquality.models.ScoreInfo;
import com.budwk.app.waterquality.services.ScoreInfoService;
import com.budwk.app.waterquality.vo.AppealRecordQueryVo;
import com.budwk.app.waterquality.vo.QueryCommonParamVo;
import com.budwk.app.waterquality.vo.ScoreInfoQueryReqVo;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@IocBean
@At("/waterquality/admin/score/info")
@ApiDefinition(tag = "评分信息表")
@SLog(tag = "评分信息表")
@Slf4j
public class ScoreInfoController {

    @Inject
    private ScoreInfoService scoreInfoService;

    @At
    @Ok("json")
    @POST
    @SLog(value = "分页查询评分信息表")
    @ApiOperation(name = "分页查询评分信息表")
    @ApiResponses(implementation = ScoreInfo.class)
    public Result<?> listByPage(@Param("..") ScoreInfoQueryReqVo reqVo) {
        return Result.data(scoreInfoService.listByPage(reqVo));
    }

    @At
    @Ok("json")
    @POST
    @SLog(value = "新增评分信息表")
    @ApiOperation(name = "新增评分信息表")
    @ApiFormParams(implementation = ScoreInfo.class)
    @ApiResponses
    public Result<?> create(@Param("..")ScoreInfo scoreInfo) {
        scoreInfoService.create(scoreInfo);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @SLog(value = "修改评分信息表")
    @ApiOperation(name = "修改评分信息表")
    @ApiFormParams(
        implementation = ScoreInfo.class
    )
    @ApiResponses
    @SaCheckPermission("waterquality.score.info.update")
    public Result<?> update(@Param("..")ScoreInfo wkScoreInfo, HttpServletRequest req) {
        wkScoreInfo.setUpdateId(SecurityUtil.getUserId());
        scoreInfoService.updateIgnoreNull(wkScoreInfo);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @SLog(value = "获取评分信息表")
    @ApiOperation(name = "获取评分信息表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("waterquality.score.info")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        ScoreInfo wkScoreInfo = scoreInfoService.fetch(id);
        if (wkScoreInfo == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wkScoreInfo);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @SLog(value = "删除评分信息表")
    @ApiOperation(name = "删除评分信息表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("waterquality.score.info.update")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        ScoreInfo wkScoreInfo = scoreInfoService.fetch(id);
        if (wkScoreInfo == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        scoreInfoService.delete(id);
        req.setAttribute("_slog_msg", wkScoreInfo.getId());
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "生成评分报告")
    public Result<?> generateRatingReport (@Param("..") ScoreInfoQueryReqVo queryVo, HttpServletResponse response) {
        try {
            return Result.data(scoreInfoService.generateRatingReport(queryVo, response));
        } catch (IOException e) {
            log.error("生成评分报告 error: ", e);
            throw new AppException("生成评分报告异常，请联系管理员！");
        }
    }
}
